#!/usr/bin/env python
#coding=utf-8
# author:wade
# contact: 317909531@qq.com
# datetime:2020/4/6 22:09

"""
文件说明：
"""
# -*- coding=utf-8
import datetime
import time

from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos import CosServiceError
from qcloud_cos import CosClientError

import sys
import logging

# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x

# pip安装指南:pip install -U cos-python-sdk-v5

# cos最新可用地域,参照https://www.qcloud.com/document/product/436/6224


def upload(File):
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    # 设置用户属性, 包括secret_id, secret_key, region
    # appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
    secret_id = 'AKID85rSr6pyDPRYVHyQnSwIH92kW8VcIq53'  # 替换为用户的secret_id
    secret_key = 'sTpujJzuYx7sNl13aszxjETRH2XBYXVI'  # 替换为用户的secret_key
    region = 'ap-chengdu'  # 替换为用户的region
    token = None  # 使用临时密钥需要传入Token，默认为空,可不填
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
    client = CosS3Client(config)

    # 文件流 简单上传
    print(File.name)
    import random
    file_name = f'{round(time.time() * 1000)}{random.randint(10000,99999)}.jpg'
    # 字节流 简单上传
    response = client.put_object(
        Bucket='abc-1253820276',
        Body=File,
        Key=file_name
    )

    # # 高级上传接口(推荐)
    # response = client.upload_file(
    #     Bucket='abc-1253820276',
    #     LocalFilePath='dd.png',
    #     Key= file_name,  # 存储名称
    #     PartSize=10,
    #     MAXThread=10
    # )
    #
    print(response['ETag'])
    print(response)
    return (file_name)

# def download():
#     logging.basicConfig(level=logging.INFO, stream=sys.stdout)
#     # 设置用户属性, 包括secret_id, secret_key, region
#     # appid已在配置中移除,请在参数Bucket中带上appid。Bucket由bucketname-appid组成
#     secret_id = 'AKID85rSr6pyDPRYVHyQnSwIH92kW8VcIq53'  # 替换为用户的secret_id
#     secret_key = 'sTpujJzuYx7sNl13aszxjETRH2XBYXVI'  # 替换为用户的secret_key
#     region = 'ap-chengdu'  # 替换为用户的region
#     token = None  # 使用临时密钥需要传入Token，默认为空,可不填
#     config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
#     client = CosS3Client(config)
#
#     # 文件流 简单上传
#     response = client.get_object(
#         Bucket='abc-1253820276',
#         Key='Desert.jpg',
#     )
#     fp = response['Body'].get_raw_stream()
#     print(fp.read(2))
#
#     response['ETag']
#     return ()

